package com.android.equipment.dao.operate;

import com.android.equipment.ObjectBox;
import com.android.equipment.dao.ConfigModel;
import com.android.equipment.dao.ConfigModel_;
import com.android.equipment.dao.Device;
import com.android.equipment.dao.Device_;

import java.util.ArrayList;
import java.util.List;

import io.objectbox.Box;
import io.objectbox.query.Query;
import io.objectbox.query.QueryBuilder;

public class DaoConfigModel {
    static Box<ConfigModel> configModelBox;

    private static DaoConfigModel instance;

    private DaoConfigModel() {
    }

    public static DaoConfigModel getInstance() {
        if (instance == null) {
            instance = new DaoConfigModel();
            configModelBox = ObjectBox.get().boxFor(ConfigModel.class);
        }
        return instance;
    }

    public void addConfigModel(ConfigModel configModel) {
        configModelBox.put(configModel);
    }

    public void addDefaultConfigModel(long deviceModelId){
        List<ConfigModel> list = new ArrayList<>();
        list.add(new ConfigModel(deviceModelId,"bat", "主机电量", "%","",0,0));
        list.add(new ConfigModel(deviceModelId,"sbat1", "电量", "%","",0,0));
        list.add(new ConfigModel(deviceModelId,"sbat2", "电量", "%","",0,0));
        list.add(new ConfigModel(deviceModelId,"sbat3", "电量", "%","",0,0));
        list.add(new ConfigModel(deviceModelId,"sbat4", "电量", "%","",0,0));
        list.add(new ConfigModel(deviceModelId,"sbat5", "电量", "%","",0,0));
        list.add(new ConfigModel(deviceModelId,"pow", "开关机状态", "","",0,0));
        list.add(new ConfigModel(deviceModelId,"check", "自检状态", "","",1,0));
        list.add(new ConfigModel(deviceModelId,"alm1", "围杆带挂钩", "","",1,0));
        list.add(new ConfigModel(deviceModelId,"alm2", "围杆带状态", "","",1,0));
        list.add(new ConfigModel(deviceModelId,"alm3", "S0S紧急求救", "","",1,0));
        list.add(new ConfigModel(deviceModelId,"alm4", "设备状态", "","",1,0));
        list.add(new ConfigModel(deviceModelId,"walm1", "保险绳挂钩", "","sbat1",1,0));
        list.add(new ConfigModel(deviceModelId,"walm2", "高挂抵用", "","sbat2",1,0));
        list.add(new ConfigModel(deviceModelId,"walm3", "绳缓冲包", "","sbat3",1,0));
        list.add(new ConfigModel(deviceModelId,"walm4", "围杆带挂钩", "","sbat4",1,0));
        list.add(new ConfigModel(deviceModelId,"walm5", "围杆带状态", "","sbat5",1,0));
        list.add(new ConfigModel(deviceModelId,"gps", "定位", "","",0,2));
        list.add(new ConfigModel(deviceModelId,"lon", "经度", "","",0,2));
        list.add(new ConfigModel(deviceModelId,"lat", "纬度", "","",0,2));
        list.add(new ConfigModel(deviceModelId,"unixTime", "时间", "","",0,0));
        list.add(new ConfigModel(deviceModelId,"high", "高度", "米","",0,2));
        configModelBox.put(list);
    }


    //获取配置项 通过 设备型号ID
    public List<ConfigModel> getConfigModelByDeviceModelId(long deviceModelId) {
        Query<ConfigModel> query = configModelBox.query(ConfigModel_.device_model.equal(deviceModelId)).build();
        List<ConfigModel> configModelList = query.find();
        query.close();
        return configModelList;
    }

    public List<ConfigModel> getShowConfigModeByDeviceModelId(long deviceModelId) {
        Query<ConfigModel> query = configModelBox
                .query()
                .equal(ConfigModel_.device_model, deviceModelId)
                .equal(ConfigModel_.is_show, 1)
                .build();
        List<ConfigModel> configModels = query.find();
        query.close();
        return configModels;
    }

    public void updateConfigModel(ConfigModel model) {
        configModelBox.put(model);
    }

    public ConfigModel getConfigModelByDeviceIdAndNode(long deviceModelId,String node){
        Query<ConfigModel> query = configModelBox
                .query()
                .equal(ConfigModel_.node,node, QueryBuilder.StringOrder.CASE_INSENSITIVE)
                .equal(ConfigModel_.device_model, deviceModelId)
                .build();
        ConfigModel configModel = query.findFirst();
        query.close();
        return configModel;
    }

}
